package com.research.web.utils;

import com.research.web.entity.UserEntity;

import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserBeanUtils {
    /*** 查询的结果集resultSet转为用户对象集合 ***/
    public static List<UserEntity> getUserEntity(ResultSet resultSet) throws Exception {
        List<UserEntity> list = new ArrayList<>();
        while (resultSet.next()) {
            list.add(UserEntity.builder()
                    .id(resultSet.getInt("id"))
                    .userName(resultSet.getString("user_name"))
                    .password(resultSet.getString("password"))
                    .updateTime(LocalDateTime.parse(resultSet.getString("update_time"),
                            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
                    .build());
        }
        return list;
    }

    /*** 单个用户信息转为用户对象集合 ***/
    public static List<UserEntity> getUserEntity(Map<String, Object> userMap) throws Exception {
        List<UserEntity> list = new ArrayList<>();
        UserEntity userEntity = new UserEntity();
        userEntity.setId(Integer.parseInt(userMap.get("id").toString()));
        userEntity.setUserName(userMap.get("userName").toString());
        userEntity.setPassword(userMap.get("password").toString());
        userEntity.setStatus(Integer.parseInt(userMap.get("status").toString()));
        userEntity.setUpdateTime(LocalDateTime.parse(userMap.get("updateTime").toString(),
                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        list.add(userEntity);
        return list;
    }
}
